package com.cskaoyan.service.user.impl;

import com.cskaoyan.bean.bo.UserListBo;
import com.cskaoyan.bean.common.BaseParam;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.User;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.po.MarketUserExample;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.user.UserManageService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author jzp
 * @since 2022/11/19 19:25
 */
@Service
public class UserManageServiceImpl implements UserManageService {
    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public CommonData<MarketUser> list(UserListBo userListBo) {
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(userListBo.getPage(), userListBo.getLimit());

        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(userListBo.getUsername())) {
            criteria.andUsernameLike("%" + userListBo.getUsername().trim() + "%");
        }
        if (!StringUtil.isEmpty(userListBo.getMobile())) {

            criteria.andMobileEqualTo(userListBo.getMobile().trim());
        }
        // 排序
        example.setOrderByClause(userListBo.getSort() + " " + userListBo.getOrder());
        // 全部查询，只要不是有where 的动态sql 就可以
        List<MarketUser> userInfo = marketUserMapper.selectByExample(example);


        // 将查询结果作为有参构造方法的形参传入，可以获得PageInfo
        PageInfo<MarketUser> pageInfo = new PageInfo<>(userInfo);
        CommonData<MarketUser> marketUserCommonData = new CommonData<>();
        marketUserCommonData.setList(pageInfo.getList());
        // 这里的total 是数据总数
        marketUserCommonData.setTotal(userInfo.size());
        marketUserCommonData.setPages(pageInfo.getPages());
        marketUserCommonData.setLimit(userListBo.getLimit());
        marketUserCommonData.setPage(userListBo.getPage());
        return marketUserCommonData;
    }

    @Override
    public MarketUser detail(User user) {

        return marketUserMapper.selectByPrimaryKey(user.getId());

    }

    @Override
    @Transactional
    public boolean update(MarketUser marketUser) {
        int num = marketUserMapper.updateByPrimaryKeySelective(marketUser);
        return num > 0;
    }
}
